import Data from '../components/Data.js'
import UserCrypto from './UserCrypto.js'

const ROOT = 'root'

async function write(uid, relPath, data){
  const enc = await UserCrypto.encrypt(uid, data)
  Data.writeJSON(relPath, enc, '\t', ROOT)
  return true
}

async function read(uid, relPath){
  const wrapper = Data.readJSON(relPath, ROOT)
  if (wrapper && wrapper.alg && wrapper.ciphertext){
    return await UserCrypto.decrypt(uid, wrapper)
  }
  return wrapper || {}
}

export default { write, read }
